package main

import (
	"b2c-delivery-optimization/internal/utils"
	"fmt"
	"log"

	"b2c-delivery-optimization/internal/test"
)

func main() {
	// 1. 连接数据库
	db, err := utils.InitDB()
	if err != nil {
		log.Fatalf("连接数据库失败: %v", err)
	}

	// 2. 创建实验运行器
	runner := test.NewExperimentRunner(db)

	// 3. 定义实验参数
	experiments := []test.ExperimentParams{
		{
			OrderCount:       100,
			MaxOrdersPerWave: 20,
			PriorityWeight:   0.7,
			LocationWeight:   0.3,
		},
		{
			OrderCount:       500,
			MaxOrdersPerWave: 50,
			PriorityWeight:   0.7,
			LocationWeight:   0.3,
		},
		{
			OrderCount:       1000,
			MaxOrdersPerWave: 100,
			PriorityWeight:   0.7,
			LocationWeight:   0.3,
		},
	}

	// 4. 运行实验
	for i, params := range experiments {
		fmt.Printf("\n运行实验 #%d:\n", i+1)
		fmt.Printf("参数: 订单数=%d, 最大波次订单数=%d, 优先级权重=%.2f, 位置权重=%.2f\n",
			params.OrderCount, params.MaxOrdersPerWave, params.PriorityWeight, params.LocationWeight)

		result, err := runner.RunExperiment(params)
		if err != nil {
			log.Printf("实验 #%d 失败: %v", i+1, err)
			continue
		}

		// 5. 输出实验结果
		printResults(result)
	}
}

func printResults(result *test.ExperimentResult) {
	fmt.Println("\n实验结果:")
	fmt.Printf("总波次数: %d\n", result.TotalWaves)
	fmt.Printf("平均波次大小: %.2f\n", result.AverageWaveSize)
	fmt.Printf("处理时间: %v\n", result.ProcessingTime)
	fmt.Printf("平均配送距离: %.2f\n", result.AverageDistance)
	fmt.Printf("优先级满足度: %.2f%%\n", result.PrioritySatisfaction)
	fmt.Println("----------------------------------------")
}
